package org.example.controller;

import org.example.common.JsonCode;
import org.example.entity.Student;
import org.example.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/student")
public class StudentController2 {

    @Autowired
    StudentService studentService;

    /*
        @PathVariable
            搭配restful风格传递参数使用
            表示将路径上的参数，转换为方法的形参
     */
    //根据id查询单条记录
    @GetMapping("/{id}")
    public Student getById(@PathVariable String id){
        System.out.println(" ------------- " + id);
        return studentService.getById(id);
    }

    @GetMapping
    public List<Student> getAll(){
        return studentService.getAll();
    }

    @PostMapping
    public JsonCode add(@RequestBody Student s){
        studentService.insert(s);
        return JsonCode.getJson();
    }

    @PutMapping
    public JsonCode update(Student s){
        studentService.update(s);
        return JsonCode.getJson();
    }

    @DeleteMapping("/{id}")
    public JsonCode delete(@PathVariable String id){
        studentService.delete(id);
        return JsonCode.getJson();
    }

}


















